<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *-------------------------------------------------------------------
 * @creator 		: 
 * @finished date	: 2009/011/09
 * @description		: Tra ve tong so dong tu cau lenh sql tuong ung
 * @access	        : public
 *
 * @param string	: $str_where_clause       : menh de where
 * 					: 
 * @return string	: $obj_result->num_rows() : tong so dong
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */	

function get_mod_section_news_home()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.csection_news,a.cimage,a.cicon ';
		$str_query .= ' FROM '.Fget_ap_table('tsection_news').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus =	1';
		$str_query .= ' ORDER BY a.cindex+0 desc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}
	
function get_section_news_byid($nid_sec)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.csection_news,a.cimage,a.ctag, a.cnote ';
		$str_query .= ' FROM '.Fget_ap_table('tsection_news').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus =	1';
		if($nid_sec != '')
			$str_query .= ' AND a.nid = '.$nid_sec;
		else
			$str_query .= ' AND a.nid is not null ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->row_array();
	}

function get_mod_cat_news($nid_sec = '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ccat_news,a.cimage';
		$str_query .= ' FROM '.Fget_ap_table('tcat_news').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		if($nid_sec != '')
			$str_query .= ' AND a.nid_section_news = '.$nid_sec;
		$str_query .= ' AND a.nstatus =	1';
		$str_query .= ' ORDER BY a.cindex+0 asc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_mod_cat_news_byid($nid_cat = '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ccat_news,a.cimage,a.ctag, a.cnote ';
		$str_query .= ' FROM '.Fget_ap_table('tcat_news').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus =	1';
		$str_query .= ' AND a.nid =	'.$nid_cat;
		$str_query .= ' ORDER BY a.ddate01 desc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->row_array();
	}

function get_mod_news_home()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ctitle,a.cshort_content,a.cimage_thumb';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus =	1';
		$str_query .= ' ORDER BY a.ddate01 desc, a.nid desc ';
		$str_query .= ' LIMIT 0,3 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}
	
function get_last_news($nid_sec	= '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ctitle,a.cshort_content,a.cimage_thumb';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		if($nid_sec != '')
			$str_query .= ' AND nid_section_news =  '.$nid_sec;
		$str_query .= ' ORDER BY a.ddate01 desc, a.nid desc ';
		$str_query .= ' LIMIT 0,5 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}
	
function get_news_by_sec($nid_sec)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ctitle,a.cshort_content,a.cimage_thumb';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid_section_news = '.$nid_sec;
		$str_query .= ' AND a.nstatus = 1 ';
		$str_query .= ' ORDER BY a.ddate01 desc ';
		$str_query .= ' LIMIT 0,6 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_news_by_cat($nid_cat)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ctitle,a.cshort_content,a.cimage_thumb';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid_cat_news = '.$nid_cat;
		$str_query .= ' AND a.nstatus = 1 ';
		$str_query .= ' ORDER BY a.ddate01 desc, a.nid desc ';
		$str_query .= ' LIMIT 0,3 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_news_byid($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid = '.$nid;
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_news_byid_detail($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid = '.$nid;
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->row_array();
	}

function get_other_news($nid,$date,$nid_cat = '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ctitle,a.cshort_content,a.cimage_thumb';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		if($nid_cat != '')
		$str_query .= ' AND a.nid_cat_news = '.$nid_cat;
		$str_query .= ' AND a.nid <> '.$nid;
		$str_query .= ' AND a.ddate01 <= "'.$date.'"';
		$str_query .= ' ORDER BY a.ddate01 desc ';
		$str_query .= ' LIMIT 0,6 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}
	

function get_page_news($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tmodule').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid = '.$nid;
		$str_query .= ' AND a.nstatus = 1 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}
	

function get_support_online()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tsponline') ;
		$str_query .= ' WHERE nid is not null ';
		$str_query .= ' AND nstatus = 1 ';
		$str_query .= ' ORDER BY cindex desc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_email_manager()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('temail_manager') ;
		$str_query .= ' WHERE nid is not null ';
		$str_query .= ' AND nstatus = 1 ';
		$str_query .= ' ORDER BY cindex desc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}
	

function get_last_news_home_active($nid_cat = '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ctitle,a.cshort_content,a.cimage_thumb, nhit';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid_cat_news = '.$nid_cat;
		$str_query .= ' AND a.nactive = 1';
		$str_query .= ' LIMIT 0,1 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_last_news_home($nid_cat = '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ctitle,a.cshort_content,a.cimage_thumb, nhit';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid_cat_news = '.$nid_cat;
		$str_query .= ' AND a.nactive = 0';
		$str_query .= ' ORDER BY a.cindex asc ';
		//$str_query .= ' LIMIT 0,1 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_popular_news_home($nid_cat = '')
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid,a.ctitle,a.cshort_content,a.cimage_thumb, nhit';
		$str_query .= ' FROM '.Fget_ap_table('tnews').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid_cat_news = '.$nid_cat;
		$str_query .= ' ORDER BY a.nhit desc, a.nid desc ';
		$str_query .= ' LIMIT 0,1 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_user_byid($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tuser').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid = '.$nid;
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_config_byid($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tconfig').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid = '.$nid;
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_cat_product_list()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tcat_products').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus = 1';
		$str_query .= ' ORDER BY cindex asc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_new_product_home()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tproducts').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus = 1';
		$str_query .= ' ORDER BY nid desc ';
		$str_query .= ' LIMIT 0,30 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}
	
function get_other_product_home()
	{
		$obj_data	= get_menu_material_mod_home();
		$str_nid	= '';
		$count		= 1;
		foreach($obj_data as $data):
			if($count == 1)
				$str_nid	= $data['nid'];
			else
				$str_nid	= $data['nid'].','.$str_nid;
			$count++;
		endforeach;

		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tproducts').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus = 1';
		$str_query .= ' AND a.nid_material_products not in ('.$str_nid.')';
		$str_query .= ' ORDER BY RAND() ';
		$str_query .= ' LIMIT 0,8 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_sale_product_home()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tproducts').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus = 1';
		$str_query .= ' AND a.fprice_sale <> 0 ';
		$str_query .= ' AND a.fprice_sale <> "" ';
		$str_query .= ' ORDER BY nid desc ';
		$str_query .= ' LIMIT 0,30 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_check_product_home()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tproducts').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus = 1';
		$str_query .= ' AND a.ncheck = 1';
		$str_query .= ' ORDER BY nid asc ';
		$str_query .= ' LIMIT 0,30 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

	
function get_last_product()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tproducts').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus = 1';
		$str_query .= ' ORDER BY ddate01 desc, nid desc ';
		$str_query .= ' LIMIT 0,3 ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_product_byid($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tproducts').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND nid = '.$nid;
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->row_array();
	}

function get_cat_product_byid($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tcat_products');
		$str_query .= ' WHERE nid is not null ';
		$str_query .= ' AND nid = '.$nid;
		$str_query .= ' AND nstatus = 1 ';	
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->row_array();
	}

function get_brand_product_byid($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tbrand_products');
		$str_query .= ' WHERE nid is not null ';
		$str_query .= ' AND nid = '.$nid;
		$str_query .= ' AND nstatus = 1 ';	
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->row_array();
	}
	
function get_cat_product_byid_material($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT a.nid, a.ccat_products, b.nid as nid_material_products,b.cmaterial_products';
		$str_query .= ' FROM '.Fget_ap_table('tcat_products').' as a, ';
		$str_query .= ' '.Fget_ap_table('tmaterial_products').' as b';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND b.nid = a.nid_material_products';
		$str_query .= ' AND b.nid = '.$nid;
		$str_query .= ' AND a.nstatus = 1 ';
		$str_query .= ' ORDER BY a.cindex asc ';	
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

	
// Module product page

function get_banner_list()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT *';
		$str_query .= ' FROM '.Fget_ap_table('tbanner_images');
		$str_query .= ' WHERE nid is not null ';
		$str_query .= ' ORDER BY cindex asc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_support_online_frontend($type	= '')
{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();
		$str=' SELECT * ';
		$str.=' FROM ' . Fget_ap_table('tsupport_online') . ' as a ';
		$str.=' WHERE a.nid is not null ';
		if($type == 1)
			$str.=' AND a.ntype = 1 '; 
		if($type == 0)
			$str.=' AND a.ntype = 0 ';  
		$str.=' AND a.nstatus = 1'; 
		$str.=' ORDER BY a.cindex DESC'; 

		$obj_result = $obj_helper->db->query($str);  
		return $obj_result->result_array();
}

function get_random_product($nid_material,$nid_cat,$nid_product)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * FROM '.Fget_ap_table('tproducts');
		$str_query .= ' WHERE nid is not null ';
		$str_query .= ' AND nid_material_products = '.$nid_material;
		$str_query .= ' AND nid_cat_products = '.$nid_cat;
		$str_query .= ' AND nid <> '.$nid_product;			
		$str_query .= ' ORDER BY RAND() ';
		$str_query .= ' LIMIT 0,8 ';	
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();

	}

function get_tab_product_by_cat($nid_cat)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * FROM '.Fget_ap_table('tproducts');
		$str_query .= ' WHERE nid is not null ';
		$str_query .= ' AND nid_cat_products = '.$nid_cat;		
		$str_query .= ' ORDER BY nid desc ';
		$str_query .= ' LIMIT 0,10 ';	
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();

	}

function get_menu_material()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tmaterial_products').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus = 1 ';		
		$str_query .= ' ORDER BY a.cindex+0 asc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_menu_material_mod_home()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tmaterial_products').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus = 1 ';	
		$str_query .= ' AND a.nspecial = 1 ';	
		$str_query .= ' ORDER BY a.cindex+0 asc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}

function get_menu_cat_by_nid_material($nid)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tcat_products').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid_material_products = '.$nid;
		$str_query .= ' AND a.nstatus = 1 ';		
		$str_query .= ' ORDER BY a.cindex asc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}	

function get_menu_brand_by_nid_cat_material($nid_mat, $nid_cat)
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tbrand_products').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nid_material_products = '.$nid_mat;
		$str_query .= ' AND a.nid_cat_products		= '.$nid_cat;
		$str_query .= ' AND a.nstatus = 1 ';		
		$str_query .= ' ORDER BY a.cindex asc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}	

function get_menu_danh_muc_khac()
	{
		$obj_helper =& get_instance(); 
		$obj_helper->load->database();	
		$str_query = ' SELECT * ';
		$str_query .= ' FROM '.Fget_ap_table('tmaterial_products').' as a ';
		$str_query .= ' WHERE a.nid is not null ';
		$str_query .= ' AND a.nstatus = 1 ';	
		$str_query .= ' ORDER BY a.cindex+0 asc ';
		$obj_result = $obj_helper->db->query($str_query);  
		return $obj_result->result_array();
	}
